#!/bin/bash

# Test md hot-plug and hot-unplug

. ./common

MD=true

if [ "$STORE" != "/tmp/sheepdog/057" ]; then
	_notrun "This test cannot be run when WD is manually set"
fi

_make_device 0 $((5 * 1024 ** 3))
_make_device 1 $((5 * 1024 ** 3))
_make_device 2 $((5 * 1024 ** 3))
_make_device 3 $((5 * 1024 ** 3))

for i in 0 1 2; do
	_start_sheep $i
done
_wait_for_sheep 3
_cluster_format
$DOG vdi create test 100M -P

_md_info

# plug during node event
_start_sheep 3
_wait_for_sheep 4
$DOG node md plug -f $STORE/0/d3,$STORE/0/d4
_wait_for_sheep_recovery 0
_md_info
$DOG vdi check test
$DOG cluster info | _filter_cluster_info

# plug duplicate path
$DOG node md plug -f $STORE/0/d3
$DOG node recovery
_md_info

# unplug
$DOG node md unplug -f $STORE/0/d0,$STORE/0/d1
_wait_for_sheep_recovery 0
_md_info
$DOG vdi check test
$DOG cluster info | _filter_cluster_info

# unplug invalid path
$DOG node md unplug -f $STORE/0/d0
$DOG node recovery
_md_info
$DOG cluster info | _filter_cluster_info

# check stale object purging
find $STORE/*/d*/.stale/ -type f | _filter_store
